/**
 * AOP 简单实现
 *
 */
var aop = (function(window, document) {

	var isFunc = function(fn) {
		return typeof fn === 'function';
	};
	var cloneFunc = function(fn) {
		return isFunc(fn) ? eval('[' + fn.toString() + ']')[0] : null;
	};
	var run = function(before, fn, after) {
		return function() {
			before.apply(this, arguments);
			var result = fn.apply(this, arguments);
			after.apply(this, arguments);
			return result;
		};
	};
	return function(context, options) {
		context = context || window;
		options = options || {};
		// Variables
		var target = options['target'],
			before = options['before'],
			after = options['after'],
			func = function() {};
		// Validates
		target = target || '*';
		if (target != '*' && !isFunc(context[target])) return;
		!isFunc(before) && (before = func);
		!isFunc(after) && (after = func);

		if (target == '*') {
			for (var t in context) {
				var fn = context[t];
				isFunc(fn) && (context[t] = run(before, cloneFunc(fn), after));
			}
		} else if (isFunc(context[target])) {
			var fn = cloneFunc(context[target]);
			context[target] = run(before, fn, after);
			return {
				rest: function() {
					context[target] = fn;
				}
			};
		}
	}
})(window, document);